package com.liuqing.www.数据库介绍.database.config;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName: AutoIncrement
 * @Description: 
 * @author kevin
 * @date 2009-6-30 下午03:21:26
 */
public class AutoIncrement {
	public AutoIncrement() {
	}
	public static long getNextID(Connection con,String sql){
		long ret = -1 ;
		
        ResultSet rs = null ;
        PreparedStatement state = null ;
        try {
        	state = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            int t = state.executeUpdate();
//            System.out.println("return rows:"+t);
            if(t > 0){
            	rs = state.getGeneratedKeys();
                if (rs.next()) {
                    ret =  rs.getLong(1);
                }
            }
        } catch (SQLException e) {
    		try {
    			if(rs != null){
    				rs.close();
    			}
    			if(state != null){
    				state.close();
    			}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
        	e.printStackTrace();
        }finally{
        	try {
    			if(rs != null){
    				rs.close();
    			}
    			if(state != null){
    				state.close();
    			}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
        }
		return ret ;
	}
}
/**begin
declare today timestamp default now();
 declare yyday timestamp ;
  declare yday timestamp ;
  declare stops int default 0;
  select date_add(sysdate() ,interval - 1 day) into yday;

 
  select date_add(sysdate() ,interval -2 day) into yyday ;


--  declare regcount int ;
--  declare channel varchar(32) ;

  start transaction;
-- reg count
  insert into log_base_reg (regcount,channel,logtime)
    select count(1) as regs ,l.channel ,yday
       from log_user_login l
       where l.`type`=3
       and date(l.logtime)= date(yday)
       group by l.channel ;
-- login count
   insert into log_base_logincount (logincount,channel,logtime)
      select count(distinct(l.loginid)) ,l.channel ,yday
       from log_user_login l
       where (l.`type`=1 or l.`type`=3)
       and date(l.logtime) = date(yday)
       group by l.channel ;
-- login max ;
   insert into log_base_loginmax (loginmax,channel,logtime)
     select count(1) ,l.channel ,yday
       from log_user_login l
       where (l.`type`=1 or l.`type`=3)
       and date(l.logtime) = date(yday)
       group by l.channel ;

-- act
    insert into log_base_act (actcount,channel,logtime)
        select count(distinct(l.loginid))+
           (
               select count(1) from (select l1.loginid
               from log_user_login l1
               where l1.`type`=1
               and date(l1.logtime) = date(yday)
               group by l1.loginid having count(1)>2) as count2
           ),
           l.channel,
           yday

           from log_user_login l
           where l.`type`=1
           and date(l.logtime) = date(yday)
           and exists (select l2.loginid
           from log_user_login l2
           where l2.`type`=3
           and date(l2.logtime) < date(yday))
           group by l.channel ;
commit;
end
 */
